package dev.loong.boot3.service.impl;

import dev.loong.boot3.pojo.User;
import dev.loong.boot3.service.RabbitMqService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RabbitMqServiceImpl implements RabbitTemplate.ConfirmCallback, RabbitMqService {
    @Value("${rabbitmq.queue.msg}")
    private String msgRouting = null;
    @Value("${rabbitmq.queue.user}")
    private String userRouting = null;

    private RabbitTemplate rabbitTemplate;
    @Autowired
    public RabbitMqServiceImpl(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }
    @Override
    public void sendMessage(String message) {
        System.out.println("发送消息：【" + message + "】");
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.convertAndSend(msgRouting, message);
    }

    @Override
    public void sendUser(User user) {
        System.out.println("发送用户：【" + user + "】");
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.convertAndSend(userRouting, user);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            System.out.println("消息发送成功");
        } else {
            System.out.println("消息发送失败" + s);
        }
    }
}
